<template>
  <el-card class="w-full pt-[5px]">
    <el-form :model="searchForm" class="mt-[20px]" label-width="110px" ref="searchFormRef">
      <el-row :gutter="20">
        <el-col :span="6" v-equalRole="['biz_kycgly','biz_kyms']">
          <el-form-item label="对冲号" class="mb-[0px]" prop="cwDch">
            <el-input v-model="searchForm.cwDch" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6" v-equalRole="['biz_kycgly','biz_kyms']">
          <el-form-item label="来款单位" class="mb-[0px]" prop="incomeUnit">
            <el-input v-model="searchForm.incomeUnit" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="摘要" class="mb-[0px]" prop="note">
            <el-input v-model="searchForm.note" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="flex justify-end">
            <el-button type="primary" @click="searchFormOpen = !searchFormOpen" v-equalRole="['biz_kycgly','biz_kyms']">
              更多
              <el-icon color="#fff">
                <CaretTop v-show="searchFormOpen" />
                <CaretBottom v-show="!searchFormOpen" />
              </el-icon>
            </el-button>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="resetForm" v-equalRole="['biz_kycgly','biz_kyms']">重置</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="mt-[20px]" v-show="searchFormOpen">             
        <el-col :span="6">
          <el-form-item label="来款类型" prop="incomeType">
            <el-select v-model="searchForm.incomeType" clearable>
              <el-option
                v-for="item in incomeTypeList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="经费状态" prop="feeStatus">
            <el-select v-model="searchForm.feeStatus" clearable>
                <el-option
                v-for="item in statusList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="0">
            <el-input
              v-model="searchForm.params.minfee"
              placeholder="最小来款金额"
              clearable
              style="width: calc(50% - 10px)"
            />
            <span style="width: 20px; text-align: center">至</span>
            <el-input
              v-model="searchForm.params.maxfee"
              placeholder="最大来款金额"
              clearable
              style="width: calc(50% - 10px)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label-width="45" >
              <CommonDateSelect
              placeholder="来款日期"
              v-model:beginTime="searchForm.params.beginTime"
              v-model:endTime="searchForm.params.endTime"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>

  <el-card class="w-full pt-[5px] mt-[10px]">
    <div class="mb-[20px]" v-equalRole="['biz_kycgly']">
      <el-button type="primary" @click="handleAdd">
        <el-icon class="mr-[5px]"><Plus /></el-icon>
        新增
      </el-button>
      <el-button @click="handleDelSelect">
        <el-icon class="mr-[5px]"><DeleteFilled /></el-icon>
        删除
      </el-button>
      <el-button @click="handleExport"> <svg-icon iconClass="export" class="mr-[5px]"></svg-icon>导出 </el-button>
    </div>
    <el-table :data="tableData" class="w-full" stripe row-key="id" ref="tableRef"
       @sort-change="sortChange">
      <el-table-column type="selection" :selectable="checkSelect" width="55" />
      <el-table-column prop="cwDch" label="对冲号" sortable="c.cw_dch" />
      <el-table-column prop="incomeUnit" label="来款单位" sortable="c.income_unit" />
      <el-table-column prop="note" label="摘要" sortable="c.note"  />
      <el-table-column prop="incomeDate" label="来款日期" sortable="c.income_date" />
      <el-table-column prop="feeValue" label="来款金额(万元)" width="135" sortable="c.fee_value" >
      <template #default="{ row }">
          <span>{{ FloatDiv(row.feeValue,10000)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="notClaimfee" label="可领金额(万元)" >
        <template #default="{ row }">
          <span>{{ FloatDiv(row.notClaimfee,10000) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="feeStatus" label="经费状态" sortable="c.fee_status" >
        <template #default="{ row }">
          <CommonLabel :dataList="statusList" :value="row.feeStatus" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center" v-equalRole="['biz_kycgly','biz_kyry']">
        <template #default ="{ row }">
          <el-button type="primary" size="small" v-equalRole="['biz_kycgly']" v-if="row.feeStatus =='0'" plain @click="handleEdit(row)" > 编辑 </el-button>
          <el-button type="primary" size="small" v-if="row.feeStatus =='0' || row.feeStatus =='1'"  plain @click="handleClaim(row)"  >认领</el-button>
          <el-button type="primary" size="small" v-equalRole="['biz_kycgly']" v-if="row.feeStatus !='0'" plain @click="handleCheck(row.id)" >查看认领</el-button>
          <el-button type="danger" size="small" v-equalRole="['biz_kycgly']" v-if="row.feeStatus =='0'"  plain  @click="handleDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-[20px] flex justify-end">
      <el-pagination
        v-bind="pagination"
        @update:current-page="currentChange"
        @update:page-size="sizeChange"
      />
    </div>
  </el-card>
  <EditModal ref="editModalRef" @save-success="search" />
  <ClaimModal ref="claimModalRef" @save-success="search" />
</template>

<script setup>
import useTable from "@/hooks/useTable";
import CommonDateSelect from "@/views/fee/components/CommonDateSelect.vue";
import CommonLabel from "@/views/fee/components/CommonLabel.vue";
import EditModal from "./components/EditModal.vue";
import ClaimModal from "./components/ClaimModal.vue";
import { getList,delincome } from "@/api/fee/rz/yhdk/index.js";
import { exportFile } from "@/api/fee/common.js";
import { downloadFile } from "@/utils/main";
import { FloatDiv } from "@/utils/pub";
import { ElMessage } from "element-plus";
import useCommonFetch from "@/hooks/useCommonFetch";
const editModalRef = ref();
const claimModalRef = ref();
const router = useRouter();
const searchFormOpen = ref(false);
const searchFormRef = ref();
const tableRef = ref();
const statusList = useCommonFetch("财务来款认领状态");//经费状态
const incomeTypeList = useCommonFetch("来款类型");; //来款类型
const searchForm = ref({
  cwDch: "",
  incomeUnit: "",
  feeStatus: "", //经费状态
  incomeType: "", //来款类型
  note:"",
  orderByColumn:"c.income_date",
  isAsc:"descending",
  params:{beginTime: "",endTime: "",maxfee:"",minfee:""},
});

const { loading, pagination, sizeChange, currentChange, tableData, handleDelete, search } =
  useTable((pagin) => 
  getList({
      pageNum: pagin.pageNum,
      pageSize: pagin.pageSize,
      ...searchForm.value,
    })
  );
//点击重置
const resetForm = () => {
  searchFormRef.value.resetFields();
  searchForm.value.params.beginTime = "";
  searchForm.value.params.endTime = "";
  searchForm.value.params.maxfee = "";
  searchForm.value.params.minfee = "";
  search();
};

//点击新增
const handleAdd = () => {
  editModalRef.value.add();
};
//点击编辑
const handleEdit = (row) => {
  editModalRef.value.edit(row);
};
//删除
const handleDel = (id) => {
  handleDelete(delincome, [id]);
};

//批量删除
const handleDelSelect = () => {
  //获取当前选中行
  const selectRows = tableRef.value.getSelectionRows();
  if (!selectRows.length) {
    ElMessage({
      message: "请选择要删除的数据！",
      type: "warning",
    });
    return;
  }
  const ids = selectRows.map((item) => item.id);
  handleDelete(delincome, ids);
};

//查看认领
const handleCheck = (id) => {
  router.push({
        path: '/fee/rz/xmrzbl/index',
        query: { id: id }
    })
}

//认领
const handleClaim= (row) => {
  claimModalRef.value.claimAction(row);
};

//排序
const sortChange =(row)=>{
 searchForm.value.orderByColumn = row.column.sortable;
 searchForm.value.isAsc=row.order;
 search();
}

//设置是否禁用复选按钮
const checkSelect= (row)=>{
  if(row.feeStatus =='0'){
    return true; //可选
  }else {
    return false;//不可选
 }
}

/** 导出按钮操作 */
const handleExport = () => {
  let params = {...searchForm.value};
  exportFile('/feemanage/cwincome/export',params).then((res) => {
    downloadFile(res, `银行到款_${new Date().getTime()}.xlsx`);
  }).catch((error) => {
    console.log(error);
  }); 
}

</script>

<style scoped lang="scss"></style>